package com.amqr.permiss6;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
 * User: LJM
 * Date&Time: 2017-01-13 & 15:49
 * Describe: Describe Text
 */
public class CallActivity extends AppCompatActivity {
    private static final int REQUESTCODE = 6001;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call);

        findViewById(R.id.mTvCall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 动态申请权限   ContextCompat.checkSelfPermission()
                if (ContextCompat.checkSelfPermission(CallActivity.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // shouldShowRequestPermissionRational   解释为什么需要这和权限
                    if (ActivityCompat.shouldShowRequestPermissionRationale(CallActivity.this,
                            Manifest.permission.CALL_PHONE)) {
                        // shouldShowRequestPermissionRationale 返回true，代表之前该权限已经被拒绝，那么针对拒绝做处理，解释为什么需要这个权限

                        new AlertDialog.Builder(CallActivity.this)
                                .setMessage("app需要开启权限才能使用此功能")
                                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        // 跳转到app设置
                                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                        intent.setData(Uri.parse("package:" + getPackageName()));
                                        startActivity(intent);
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    } else {
                        // 弹出权限申请框框
                        ActivityCompat.requestPermissions(CallActivity.this,
                                new String[]{Manifest.permission.CALL_PHONE},
                                REQUESTCODE);
                    }

                } else {
                    callNum();
                }
            }
        });
    }


    private void callNum() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "10010");
        intent.setData(data);
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUESTCODE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callNum();
                } else {
                }
                return;
            }
        }
    }
}
